package com.esun.payment_api.controller;

import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.PjBean;
import com.esun.payment_api.bean.SumDkReceiveMoneyBean;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.dto.PInvInsParam;
import com.esun.payment_api.dto.PjQueryParam;
import com.esun.payment_api.service.FkDraftClaimService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.esun.common.utils.JWTUtils.getLoginUser;

/**
 * 汇票付款-关联已认领票据
 * Create by hemeichong on 2023-06-12
 */
@RestController
@RequestMapping("/fkDraftClaim")
public class FkDraftClaimController {

    @Resource
    private FkDraftClaimService fkDraftClaimService;

    @ApiOperation("汇票付款-关联票据：根据Id删除票据")
    @RequestMapping ("/deleteById")
    public CommonResult deleteById(Integer id) {
        try {
            int count = fkDraftClaimService.deleteById(id);
            if (count > 0) {
                return CommonResult.success(count);
            } else {
                return CommonResult.failed("删除失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    //汇票付款-关联票据：根据id返回票据

    @ApiOperation("汇票付款-关联票据：新增多条")
    @RequestMapping("/insertMultiple")
    public CommonResult insertMultiple(@RequestBody PInvInsParam param) {
        try {
            System.out.println(" idStr: " + param.getIdStr().replaceAll(",", ":"));
            param.setIdStr(param.getIdStr().replaceAll(",", ":"));
            System.out.println("idStr="+param.getIdStr()+"|fkId="+param.getFkId()+"|coId="+param.getCoId()+"|userId="+param.getUserId());
            fkDraftClaimService.insertMultiple(param);
            return CommonResult.success("新增任务号成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("汇票付款-关联票据：根据付款id获取可选择票据列表")
    @GetMapping("/getFkSelectPjList")
    @ResponseBody
    public CommonResult getFkSelectPjList(PjQueryParam queryParam,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        /**
         * 入参说明：
         *  fkId        主键id
         *  searchStr   模糊搜索词
         *  startMoney  开始金额
         *  endMoney    结束金额
         *  startTime   开始时间，格式'2023-07-11'
         *  endTime     结束时间，格式'2023-08-11'
         *  lb          票据类别
         */
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            return CommonResult.failed("无法从token中获取登录用户信息");
        }
        if (BlankUtils.isBlank(user.getCoId())) {
            return CommonResult.failed("核算单位id不能为空");
        }
        if (BlankUtils.isBlank(queryParam.getFkId())) {
            return CommonResult.failed("付款id不能为空");
        }
        queryParam.setCoId(user.getCoId());
        //如果开始时间传入为''，设置为空
        if (BlankUtils.isBlank(queryParam.getStartTime())){
            queryParam.setStartTime(null);
        }
        if (BlankUtils.isBlank(queryParam.getEndTime())){
            queryParam.setEndTime(null);
        }
        if (BlankUtils.isBlank(queryParam.getLb())){
            queryParam.setLb(null);
        }

        SumDkReceiveMoneyBean sumMoney = fkDraftClaimService.getFkSelectPjZje(queryParam);
        List<PjBean> list = fkDraftClaimService.getFkSelectPjList(queryParam, pageSize, pageNum);
        CommonPage<PjBean> pjBeanCommonPage = CommonPage.restPage(list);
        //可选票据总金额
        pjBeanCommonPage.setTotalInvMoney(sumMoney.getHpTotalMoney());
        return CommonResult.success(pjBeanCommonPage, "获取可选择票据列表成功");
    }

    @ApiOperation("汇票付款-关联票据：确认按钮")
    @RequestMapping ("/confirmButton")
    public CommonResult confirmButton(Integer id) {
        try {
            int count = fkDraftClaimService.confirmButton(id);
            if (count > 0) {
                return CommonResult.success("确认成功");
            } else {
                return CommonResult.failed("确认失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @ApiOperation("汇票付款-关联票据：取消按钮")
    @RequestMapping ("/cancelButton")
    public CommonResult cancelButton(Integer id) {
        try {
            int count = fkDraftClaimService.cancelButton(id);
            if (count > 0) {
                return CommonResult.success("取消失败");
            } else {
                return CommonResult.failed("取消失败");
            }
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

}
